#include<string>
#include<vector>
#include<array>
#include<unordered_map>
#include<algorithm>
#include<limits>
#include <stack>
#include<iostream>
#include<sstream>
#include<deque>
#include<queue>
#include<set>
#include<unordered_set>
#define MOD 1000000007

class Solution
{
public:
    int longestCommonSubsequence(std::string text1, std::string text2)
    {
        //return  f(text1,text2,text1.size(),text2.size());
        std::vector<std::vector<int>> dp(text1.size() + 1, std::vector<int>(text2.size() + 1, 0));
        dp[0][0] = 0;
        int n = text1.size();
        int m = text2.size();
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (text1[i - 1] == text2[j - 1])
                {
                    dp[i][j] = 1 + dp[i - 1][j - 1];
                    continue;
                }
              
                dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
        return dp[n][m];
    }

    int f(const std::string& text1, const std::string& text2, int l1, int l2)
    {
        if (l1 == 0 || l2 == 0) return 0;
        if (text1[l1 - 1] == text2[l2 - 1])
        {
            return f(text1, text2, l1 - 1, l2 - 1) + 1;
        }
        return std::max(f(text1, text2, l1 - 1, l2), f(text1, text2, l1, l2 - 1));
    }
};
